<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"

    width="50%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :data="formData" labelWidth="100px" :rules="rules">
        <t-form-item label="标题" name="title">
          <t-input clearable v-model="formData.title" placeholder="请输入标题"></t-input>
        </t-form-item>
        <t-form-item label="说明" name="note">
          <tiny-mce-editor v-model="formData.note"></tiny-mce-editor>
        </t-form-item>
        <t-form-item label="是否发布" name="isPublish">
          <t-radio-group :options="dict.type.yes_or_not" v-model="formData.isPublish"></t-radio-group>
        </t-form-item>
        <t-form-item label="发布日期" name="publishDate">
          <t-date-picker  v-model="formData.publishDate"></t-date-picker>
        </t-form-item>

        <t-form-item label="附件上传" name="sysAttMainIds">
          <file-upload tips="" :fileList="fileLists" v-model="formData.sysAttMainVo.sysAttMainIds"></file-upload>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";
import TinyMceEditor from "@/components/TinyMceEditor/index.vue";


export default {
  name: "add-edit-notice",
  components:{
    FileUpload,
    TinyMceEditor
  },
  props: ['header', 'objId', 'parentId'],
  dicts: ['yes_or_not'],
  data(){
    return {
      visible: true,
      loading: false,
      fileLists: [],
      formData:{
        title: '',
        isPublish: '1',
        note: '',
        publishDate: '',
        sysAttMainVo: {
          sysAttMainIds: []
        }
      },
      rules:{
        title: [
          {required: true, message: "标题不能为空"}
        ],
        isPublish: [
          {required: true, message: "不能为空"}
        ],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj() {
      this.$api.wlaq.notice.get(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        Object.assign(this.formData, response.data)
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.wlaq.notice.update(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.wlaq.notice.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
